package com.anim.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.MouseListenerAdapter;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.Widget;

public class MoonDeneme implements EntryPoint{
	

	private AbsolutePanel map;				// Permits abs. pos. of widgets
	private AbsolutePanel mapContaierPan;	// Permits abs. pos. of widgets
	private FocusPanel focPan;				// Permits mouse event capture
	private Image character;					// Duke image
	int charDirection;
	private Timer charTimer;
	
	private Image grid;
	int gridPosX,gridPosY=0;
	
	private Label Mx,My;					// Print mouse positions to screen
	private Label dMx,dMy;					// Print Duke positions to screen
	
	private int charPosOnMapX=0;				// duke position x on map
	private int charPosOnMapY=-52;				// duke position y on map
	
	private int ClickPositionForCharX=528;			// Click position on the map X
	private int ClickPositionForCharY=396;			// Click position on the map Y
	
	private int mapPositionX=0;				// Map position X
	private int mapPositionY=0;				// Map position Y
	
	private int mouseOnMapPositionX=0;		// Mouse position on map X
	private int mouseOnMapPositionY=0;		// Mouse position on map Y
	
	
	/*Move Map's Booleans if one of them is true map will move to its direction*/
	private boolean mapMoveRight=false;
	private boolean mapMoveLeft=false;
	private boolean mapMoveUp=false;
	private boolean mapMoveDown=false;	
	
	int lenghtOfTileOnXDirection=40;
	int lenghtOfTileOnYDirection=30;
	
	
	int startX=lenghtOfTileOnXDirection*48;				//start position of creation of tiles   X/ size * 48  EX: 48*20=960 ,20 tiles in x direction*/
	int startY=0;
	
	int mainGameWindowSizeWidth=640;		//game window size W
	int mainGameWindowSizeHeight=480;		//game window size H
	/*
	 * info of Tile size is from  http://wiki.fifengine.de/Map_GFX_contest#Requirements_for_the_graphics
	 * 
	 * 
	 */

	public void onModuleLoad() {
		loadImgResource();
		
		mapContaierPan=new AbsolutePanel();
		mapContaierPan.setPixelSize(mainGameWindowSizeWidth, mainGameWindowSizeHeight);
		mapContaierPan.setStyleName("outerAbsolutePanel");
		

		map = new AbsolutePanel();		// Create panel for absolute
		
		Mx=new Label("X");
		My=new Label("Y");
		dMx=new Label("X");
		dMy=new Label("Y");
		/*
		 * CREATE tiles
		 * */

		for(int i=0;i<lenghtOfTileOnYDirection+1;i++){		/*    Y  \ 	*/
			for(int j=0;j<lenghtOfTileOnXDirection+1;j++){ 	/*    X /  	*/
				Image tile=new Image("./brda000.gif");
				map.add(tile);
				map.setWidgetPosition(tile, startX-(j*48),startY+(j*12)  );
			}
			startX+=32;
			startY+=24;
		}
		map.setPixelSize(startX+48, startY+(12*lenghtOfTileOnXDirection)+12);
		
		
		map.setStyleName("innerAbsolutePanel");
		mapContaierPan.add(map);
		focPan = new FocusPanel();			// Place it inside a panel for
		focPan.add(mapContaierPan);					// ...mouse event capture
	
		focPan.addMouseListener(new MouseListenerAdapter()
		{	
			public void onMouseUp(Widget w,int x,int y) {
				updateDuke(x,y);
			}
			public void onMouseMove(Widget w,int x,int y){

				
				SetGridPosition(x+mouseOnMapPositionX,y+mouseOnMapPositionY);
				
				if(x>mainGameWindowSizeWidth-40){
					mapMoveRight=true;
				}
				if(x<mainGameWindowSizeWidth-40){
					mapMoveRight=false;
				}
				
				if(x<40){
					mapMoveLeft=true;
				}
				if(x>40){	
					mapMoveLeft=false;
				}

				if(y>mainGameWindowSizeHeight-40){
					mapMoveDown=true;
				}
				if(y<mainGameWindowSizeHeight-40){
					mapMoveDown=false;
				}
				
				if(y<40){
					mapMoveUp=true;
				}
				if(y>40){
					mapMoveUp=false;
				}
			}
		
		});
		character = new Image("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-2-00.gif");		
		charDirection=5;
		character.setStyleName("dukee");
		map.add(character);
		map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );



		RootPanel.get("main").add(focPan);		// Hook it all up to page
		RootPanel.get("x").add(Mx);
		RootPanel.get("y").add(My);
		RootPanel.get("dx").add(dMx);
		RootPanel.get("dy").add(dMy);
		grid=new Image("./MSEF000.gif");
		map.add(grid);
		map.setWidgetPosition(grid, 0, 0);
		new AnimationTimer().scheduleRepeating(1); // Start anim loop
		
		charTimer=new Timer(){
		      @Override
		      public void run() {
		    	  if(ClickPositionForCharX>charPosOnMapX && ClickPositionForCharY>charPosOnMapY)
					{	
		    		  	charDirection=2;
						charPosOnMapX=charPosOnMapX+4;
						charPosOnMapY=charPosOnMapY+3;
						if((charPosOnMapX/4)%8==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-0.gif");//24
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-1.gif");//27
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==4){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-2.gif");//29
							map.setWidgetPosition( character, charPosOnMapX-3, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-3.gif");//27
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==6){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-4.gif");//24
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==7){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-5.gif");//20
							map.setWidgetPosition( character, charPosOnMapX+2, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-6.gif");//30
							map.setWidgetPosition( character, charPosOnMapX-3, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==1){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-7.gif");//22
							map.setWidgetPosition( character, charPosOnMapX+1, charPosOnMapY );
						}

					}
		    	  else if(ClickPositionForCharX<charPosOnMapX && ClickPositionForCharY<charPosOnMapY)
					{
		    		  	charDirection=5;
						charPosOnMapX=charPosOnMapX-4;
						charPosOnMapY=charPosOnMapY-3;
						if((charPosOnMapX/4)%8==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-7.gif");//21
							map.setWidgetPosition( character, charPosOnMapX+2, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-6.gif");//30
							map.setWidgetPosition( character, charPosOnMapX-3, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==4){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-5.gif");//22
							map.setWidgetPosition( character, charPosOnMapX+1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-4.gif");//26
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==6){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-3.gif");//27
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==7){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-2.gif");//29
							map.setWidgetPosition( character, charPosOnMapX-3, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-1.gif");//27
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==1){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-0.gif");//24
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
					}
		    	  else if(ClickPositionForCharX<charPosOnMapX && ClickPositionForCharY>charPosOnMapY)
					{	
		    		  	charDirection=3;
						charPosOnMapX=charPosOnMapX-4;
						charPosOnMapY=charPosOnMapY+3;
						if((charPosOnMapX/4)%8==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-0.gif");//26 62
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-1.gif");//24 63
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==4){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-2.gif");//29 65
							map.setWidgetPosition( character, charPosOnMapX-3, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-3.gif");//24 64
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==6){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-4.gif");//25 61
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY+1 );
						}
						if((charPosOnMapX/4)%8==7){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-5.gif");//26 62
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-6.gif");//26 66
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY-1 );
						}
						if((charPosOnMapX/4)%8==1){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-7.gif");//28 64
							map.setWidgetPosition( character, charPosOnMapX-2, charPosOnMapY );
						}

					}
		    	  
		    	  else if(ClickPositionForCharX>charPosOnMapX && ClickPositionForCharY<charPosOnMapY)
					{	
		    		  	charDirection=0;
						charPosOnMapX=charPosOnMapX+4;
						charPosOnMapY=charPosOnMapY-3;
						if((charPosOnMapX/4)%8==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-7.gif");//27 66
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY-1 );
						}
						if((charPosOnMapX/4)%8==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-6.gif");//27 68
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY-2 );
						}
						if((charPosOnMapX/4)%8==4){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-5.gif");//27 66
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY-1 );
						}
						if((charPosOnMapX/4)%8==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-4.gif");//25 64
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==6){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-3.gif");//23 65
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==7){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-2.gif");//30 65
							map.setWidgetPosition( character, charPosOnMapX-3, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-1.gif");//23 64
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/4)%8==1){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-0.gif");//26 63                 
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}

					}
		    	  else if(ClickPositionForCharX>charPosOnMapX)
					{	
		    		  	charDirection=1;
						charPosOnMapX=charPosOnMapX+8;
						if((charPosOnMapX/8)%8==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-0.gif");//18 62
							map.setWidgetPosition( character, charPosOnMapX+3, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-1.gif");//25 62
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==4){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-2.gif");//43 60
							map.setWidgetPosition( character, charPosOnMapX-10, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-3.gif");//27 63
							map.setWidgetPosition( character, charPosOnMapX-1, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==6){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-4.gif");//18 63
							map.setWidgetPosition( character, charPosOnMapX+3, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==7){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-5.gif");//23 65
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-6.gif");//43 61
							map.setWidgetPosition( character, charPosOnMapX-10, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==1){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-7.gif");//23 63                 
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}

					}
		    	  else if(ClickPositionForCharX<charPosOnMapX)
					{	
		    		  	charDirection=4;
						charPosOnMapX=charPosOnMapX-8;
						if((charPosOnMapX/8)%8==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-7.gif");//23 62
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-6.gif");//43 62
							map.setWidgetPosition( character, charPosOnMapX-10, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==4){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-5.gif");//23 60
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-4.gif");//18 63
							map.setWidgetPosition( character, charPosOnMapX+3, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==6){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-3.gif");//28 63
							map.setWidgetPosition( character, charPosOnMapX-2, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==7){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-2.gif");//43 65
							map.setWidgetPosition( character, charPosOnMapX-10, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-1.gif");//25 61
							map.setWidgetPosition( character, charPosOnMapX, charPosOnMapY );
						}
						if((charPosOnMapX/8)%8==1){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-0.gif");//18 63                 
							map.setWidgetPosition( character, charPosOnMapX+3, charPosOnMapY );
						}

					}
		    	  
					else{
						if(charDirection==0){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-0-00.gif");
						}
						if(charDirection==1){
							map.setWidgetPosition( character, charPosOnMapX+10, charPosOnMapY );
							character.setUrl("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-1-00.gif");
						}
						if(charDirection==2){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-2-00.gif");
						}
						if(charDirection==3){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-3-00.gif");
						}
						if(charDirection==4){
							map.setWidgetPosition( character, charPosOnMapX+10, charPosOnMapY );
							character.setUrl("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-4-00.gif");
						}
						if(charDirection==5){
							character.setUrl("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-5-00.gif");
						}
			    	} 	   
		      }
		};
		charTimer.scheduleRepeating(100);
		    
	}



	
	private void loadImgResource() {
		// TODO Auto-generated method stub

		Image.prefetch("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-0-00.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-1-00.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-2-00.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-3-00.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-4-00.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAA/HMMAXXAA.gif-5-00.gif");
		
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-0.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-1.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-2.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-3.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-4.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-5.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-6.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-0-7.gif");
		
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-0.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-1.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-2.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-3.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-4.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-5.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-6.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-1-7.gif");
		
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-0.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-1.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-2.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-3.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-4.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-5.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-6.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-2-7.gif");
		
		
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-0.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-1.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-2.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-3.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-4.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-5.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-6.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-3-7.gif");
		
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-0.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-1.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-2.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-3.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-4.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-5.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-6.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-4-7.gif");
		
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-0.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-1.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-2.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-3.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-4.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-5.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-6.gif");
		Image.prefetch("./gfx/critter/art/critters/HMMAXXAB/HMMAXXAB.gif-5-7.gif");
		
	}




	protected void SetGridPosition(int mx, int my) {
		//referance : http://weblogs.java.net/blog/malenkov/archive/2009/02/hexagonal_tile_1.html
		int H=12;
		int R=16;
		int W=32;
		int L=4;
		int j = (int) (my / H);
		int x = mx + ( j * R);
		int i = (int) (x / W);

		my -= (double) j * H;
		if (my < L) {
			x -= (double) i * W + R;
			x *= L / R;
			if (my < Math.abs(x)) {
				j--;
				if (x < 0.0) {
					i--;
				}
			}
		}
		Mx.setText("GridX:"+i);
		My.setText("GridY:"+j);

		gridPosX=((i*32)-(j*16));
		gridPosY=(j*12);
		
		dMx.setText("X:"+gridPosX);
		dMy.setText("Y:"+gridPosY);
		
		map.setWidgetPosition(grid, gridPosX,gridPosY);

	}




	private void updateDuke(int x,int y)			// Update Duke / shadow alpha
	{	
			ClickPositionForCharX=gridPosX;			/*-16+16 dif of width*/
			ClickPositionForCharY=gridPosY-52;		/*-60+8  dif of height*/
	}
	
	
	
	private class AnimationTimer extends Timer
	{	public void run()
		{	
			if(mapMoveRight && mapPositionX>-(startX+48-mainGameWindowSizeWidth)){
				mapPositionX-=5;
				mouseOnMapPositionX+=5;
				mapContaierPan.setWidgetPosition(map, mapPositionX, mapPositionY);
			}
			if(mapMoveLeft && mapPositionX<0) {
				mapPositionX+=5;
				mouseOnMapPositionX-=5;
				mapContaierPan.setWidgetPosition(map, mapPositionX, mapPositionY);
			}
			if(mapMoveDown && mapPositionY>-(startY+(12*lenghtOfTileOnXDirection)+12-mainGameWindowSizeHeight)){
				mapPositionY-=5;
				mouseOnMapPositionY+=5;
				mapContaierPan.setWidgetPosition(map, mapPositionX, mapPositionY);
			}
			if(mapMoveUp && mapPositionY<0){
				mapPositionY+=5;
				mouseOnMapPositionY-=5;
				mapContaierPan.setWidgetPosition(map, mapPositionX, mapPositionY);
			}
			
		}
	}
}
